package com.yuyang.wifi.activitys.web;

import android.os.Build;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yuyang.wifi.R;
import com.yuyang.wifi.base.BaseActivity;
import com.yuyang.wifi.views.NormalWhiteTopBar;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import butterknife.BindView;

public class WebViewSecretActivity extends BaseActivity {
    @BindView(R.id.tool_bar)
    NormalWhiteTopBar toolBar;
    @BindView(R.id.web_view)
    WebView mWebView;
    private String url;

    @Override
    protected void initActionBar() {
        toolBar.setActionBarCentreTitle("");
        toolBar.setsetActionBarBottomLine(false);
        toolBar.setsetActionBarLeftIcon(R.mipmap.toolbar_left_arrow_back_icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_secret_web_view;
    }

    @Override
    protected void initContentView() {

        url = getIntent().getStringExtra("url");
        String title = getIntent().getStringExtra("title");
        toolBar.setActionBarCentreTitle(title);
        initWebView();
    }

    private void initWebView() {
//        mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
//        mWebView.getSettings().setJavaScriptEnabled(true);
//        mWebView.addJavascriptInterface(new JavaScriptInterface(this), "android");
//        mWebView.getSettings().setUseWideViewPort(true);
//        mWebView.getSettings().setLoadWithOverviewMode(true);
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
//            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//        }
//        mWebView.setWebViewClient(new WebViewClient() {
//
//
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                dismissDialog();
//            }
//
//
//            @Override
//            public void onReceivedError(WebView view, int errorCode,
//                                        String description, String failingUrl) {
//                dismissDialog();
//                mWebView.setVisibility(View.GONE);
//                ToastUtils.showShortToast(getApplicationContext(), "请求失败，请退出重试");
//
//            }
//
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }
//        });
//        mWebView.setWebChromeClient(new HarlanWebChromeClient(this));


        try {//本地HTML里面有跨域的请求 原生webview需要设置之后才能实现跨域请求
            mWebView.getSettings().setDefaultTextEncodingName("utf-8");//文本编码
            // mWebView.setDomStorageEnabled(true);//设置DOM存储已启用（注释后文本显示变成js代码）
            if (Build.VERSION.SDK_INT >= 16) {
                Class<?> clazz = mWebView.getSettings().getClass();
                Method method = clazz.getMethod(
                        "setAllowUniversalAccessFromFileURLs", boolean.class);
                if (method != null) {
                    method.invoke(mWebView.getSettings(), true);
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

    }
}
